package com.lsmgc.cms.servlet.classify;

import com.alibaba.fastjson.JSON;
import com.lsmgc.cms.entity.classify.Classify;
import com.lsmgc.cms.entity.classify.ClassifyQuery;
import com.lsmgc.cms.service.classify.ClassifyService;
import com.lsmgc.cms.util.RequestUtils;
import com.lsmgc.cms.util.ServletUtils;
import com.lsmgc.cms.util.json.JsonReulst;
import com.lsmgc.cms.util.page.Pagination;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AdminClassifyServlet extends HttpServlet {

    private ClassifyService classifyService = new ClassifyService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        switch (path) {
            case "/admin/classify/list":
                try {
                    classifyList(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/classify/add":
                classifyAdd(request, response);
                break;
            case "/admin/classify/upd":
                try {
                    classifyUpd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/classify/list":
                ajaxClassifyList(request, response);
                break;
            case "/admin/ajax/classify/add":
                try {
                    ajaxClassifyAdd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/classify/upd":
                try {
                    ajaxClassifyUpd(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "/admin/ajax/classify/del":
                try {
                    ajaxClassifyDel(request, response);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void classifyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        ClassifyQuery classifyQuery = new ClassifyQuery();
        classifyQuery.setName(request.getParameter("name"));
        classifyQuery.setPageNo(Pagination.cpn(RequestUtils.getInt(request,"pageNo")));
        classifyQuery.setPageSize(5);
        Pagination pagination = classifyService.getClassifyList(classifyQuery);
        request.setAttribute("pagination", pagination);
        request.setAttribute("classifyQuery", classifyQuery);
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/classify/classify-list")).forward(request, response);
    }

    private void classifyAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/classify/classify-add")).forward(request, response);
    }

    private void classifyUpd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Long id = RequestUtils.getLong(request, "id");
        Classify classify = classifyService.getClassifyById(id);
        request.setAttribute("classify", classify);
        request.getRequestDispatcher(ServletUtils.getJspPage("admin/classify/classify-upd")).forward(request, response);
    }

    private void ajaxClassifyList(HttpServletRequest request, HttpServletResponse response) {

    }

    private void ajaxClassifyAdd(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Classify classify = new Classify();
        classify.setName(request.getParameter("name"));
        classify.setImg(request.getParameter("img"));
        classify.setAddTime(new Date());
        boolean b = classifyService.addClassify(classify);
        PrintWriter writer = response.getWriter();
        if (b) {
            writer.write(JSON.toJSONString(JsonReulst.yes()));
        } else {
            writer.write(JSON.toJSONString(JsonReulst.no()));
        }
    }

    private void ajaxClassifyUpd(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Classify classify = new Classify();
        classify.setId(RequestUtils.getLong(request, "id"));
        classify.setName(request.getParameter("name"));
        classify.setImg(request.getParameter("img"));
        boolean b = classifyService.updateClassifyById(classify);
        PrintWriter writer = response.getWriter();
        if (b) {
            writer.write(JSON.toJSONString(JsonReulst.yes()));
        } else {
            writer.write(JSON.toJSONString(JsonReulst.no()));
        }
    }

    private void ajaxClassifyDel(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Long id = RequestUtils.getLong(request, "id");
        boolean b = classifyService.deleteClassifyById(id);
        PrintWriter writer = response.getWriter();
        if (b) {
            writer.write(JSON.toJSONString(JsonReulst.yes()));
        } else {
            writer.write(JSON.toJSONString(JsonReulst.no()));
        }
    }
}
